
import { DefaultLayout, GraphNode } from "@/js/components/GraphNode"
import { GraphView } from "@/js/components/GraphView"
import { InputLinkNodeBoolean, InputLinkNodeFlow } from "@/js/components/InputLinkNode"

export class If extends GraphNode{
    layout?: DefaultLayout
    load(){
        super.load()

        const layout = new this.DefaultLayout(this, this.graphView as GraphView)
        this.layout = layout
        this.appendChild(layout.container)

        layout.main?.left.appendChild(new InputLinkNodeBoolean(this, this.graphView as GraphView, {
            name: "condition",
            desc: '条件',
            model: "entry",
        }))

        layout.main?.right.appendChild(new InputLinkNodeFlow(this, this.graphView as GraphView, {
            name: "true", 
            desc: '真',
            model: "output",
            showInput: false,
        }))
    }
}

If.parser = ({node, toNode})=>{
    const next = toNode(node.outputs.output[0], 'enter')
    const trueBody = toNode(node.outputs.true[0], 'enter')
    const condition = toNode(node.enters.condition[0], 'output')

    return `if(${condition || node.userData.condition || node.userData.enters.condition || 'false'}){${trueBody}}${next}`
}